package com.zenithmind.article.pojo.domain;

import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Min;

/**
 * 评论聚合根
 * 遵循单一职责原则：专注于评论业务逻辑
 * 遵循封装原则：通过方法控制状态变更
 * 遵循开闭原则：通过枚举支持状态扩展
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@Schema(name = "评论实体", description = "评论聚合根")
public class Comment extends BaseEntity {

    @NotBlank(message = "文章ID不能为空")
    @Schema(description = "文章ID")
    private String articleId;

    @NotBlank(message = "评论用户ID不能为空")
    @Schema(description = "评论用户ID")
    private String userId;

    @Schema(description = "父评论ID")
    private String parentId;

    @NotBlank(message = "评论内容不能为空")
    @Size(max = 1000, message = "评论内容长度不能超过1000个字符")
    @Schema(description = "评论内容")
    private String content;

    @Min(value = 0, message = "点赞数不能为负数")
    @Schema(description = "点赞数")
    private Integer likeCount = 0;

    @Schema(description = "评论状态")
    private CommentStatus status = CommentStatus.PENDING;

    @Schema(description = "回复数量")
    private Integer replyCount = 0;

    /**
     * 评论状态枚举
     */
    public enum CommentStatus {
        PENDING(0, "待审核"),
        APPROVED(1, "已通过"),
        REJECTED(2, "已拒绝"),
        DELETED(3, "已删除");

        private final Integer code;
        private final String description;

        CommentStatus(Integer code, String description) {
            this.code = code;
            this.description = description;
        }

        public Integer getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static CommentStatus fromCode(Integer code) {
            if (code == null) return PENDING;
            for (CommentStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return PENDING;
        }
    }

    // === 构造方法 ===

    /**
     * 创建新评论
     */
    public Comment(String articleId, String userId, String content, String parentId) {
        this.articleId = articleId;
        this.userId = userId;
        this.content = content;
        this.parentId = parentId;
        this.likeCount = 0;
        this.status = CommentStatus.PENDING;
        this.replyCount = 0;
    }

    // === 业务方法 ===

    /**
     * 审核通过
     */
    public void approve() {
        if (this.status == CommentStatus.DELETED) {
            throw new IllegalStateException("已删除的评论不能审核");
        }
        this.status = CommentStatus.APPROVED;
    }

    /**
     * 审核拒绝
     */
    public void reject() {
        if (this.status == CommentStatus.DELETED) {
            throw new IllegalStateException("已删除的评论不能审核");
        }
        this.status = CommentStatus.REJECTED;
    }

    /**
     * 删除评论
     */
    public void delete() {
        this.status = CommentStatus.DELETED;
    }

    /**
     * 增加点赞数
     */
    public void incrementLikeCount() {
        this.likeCount++;
    }

    /**
     * 减少点赞数
     */
    public void decrementLikeCount() {
        if (this.likeCount > 0) {
            this.likeCount--;
        }
    }

    /**
     * 增加回复数
     */
    public void incrementReplyCount() {
        this.replyCount++;
    }

    /**
     * 减少回复数
     */
    public void decrementReplyCount() {
        if (this.replyCount > 0) {
            this.replyCount--;
        }
    }

    /**
     * 更新评论内容
     */
    public void updateContent(String content) {
        if (this.status == CommentStatus.DELETED) {
            throw new IllegalStateException("已删除的评论不能修改");
        }
        if (this.status == CommentStatus.APPROVED) {
            // 已审核通过的评论修改后需要重新审核
            this.status = CommentStatus.PENDING;
        }
        this.content = content;
    }

    /**
     * 检查是否为根评论
     */
    public boolean isRootComment() {
        return this.parentId == null || this.parentId.isEmpty();
    }

    /**
     * 检查是否为回复评论
     */
    public boolean isReply() {
        return !isRootComment();
    }

    /**
     * 检查评论是否已审核通过
     */
    public boolean isApproved() {
        return this.status == CommentStatus.APPROVED;
    }

    /**
     * 检查评论是否可以编辑
     */
    public boolean isEditable() {
        return this.status != CommentStatus.DELETED;
    }

    /**
     * 检查评论是否可以回复
     */
    public boolean canReply() {
        return this.status == CommentStatus.APPROVED;
    }

    /**
     * 获取状态码（用于数据库存储）
     */
    public Integer getStatusCode() {
        return this.status.getCode();
    }

    /**
     * 设置状态（用于数据库读取）
     */
    public void setStatusCode(Integer statusCode) {
        this.status = CommentStatus.fromCode(statusCode);
    }
}
